package model;

import javax.xml.parsers.*;
import org.w3c.dom.*;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

import java.util.*;
import java.io.*;

/**
 * Klasa pomocu koje se ucitavaju opcije iz datoteke, ili spremaju u nju.
 * 
 * @author Hrvoje Torbasinovic
 * @version 0.1
 */
public class ParseConfig {
   
    private DocumentBuilder db;
    
    /**
     * Instancira novi ParseConfig objekt
     */
    public ParseConfig ()
    {
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try
        {
        	db = dbf.newDocumentBuilder();	
        } catch (Exception e)
        {
        	e.printStackTrace();
        	return;
        }
    }
    
    /**
     * Ucitava opcije iz xml datoteke. Opcije koje ucitava su driverClassName,
     * protocol, host, port, serverName, dbName, DB_LOCALE, NODEFDAC,
     * IFX_ISOLATION_LEVEL i IFX_LOCK_MODE_WAIT
     * 
     * @param f datoteka u kojoj se nalaze opcije
     * 
     * @return mapa u kojoj se nalaze opcije
     */
    public LinkedHashMap<String,String> load(File f)
    {
    	Node n;
    	LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        Document dom;
        
        try
        {
        	dom = db.parse(new FileInputStream(f));
        } catch (Exception e)
        {
        	e.printStackTrace();
        	return null;
        }
        
        Element rootElement = dom.getDocumentElement();
        n = rootElement.getElementsByTagName("driverClassName").item(0);
        if (n != null)
        	options.put("driverClassName", n.getTextContent());
        
        n = rootElement.getElementsByTagName("protocol").item(0);
        if (n != null)
        	options.put("protocol", n.getTextContent());
        
        n = rootElement.getElementsByTagName("host").item(0);
        if (n != null)
        	options.put("host", n.getTextContent());
        
        n = rootElement.getElementsByTagName("port").item(0);
        if (n != null)
        	options.put("port", n.getTextContent());
        
        n = rootElement.getElementsByTagName("serverName").item(0);
        if (n != null)
        	options.put("serverName", n.getTextContent());
        
        n = rootElement.getElementsByTagName("dbName").item(0);
        if (n != null)
        	options.put("dbName", n.getTextContent());
        
        n = rootElement.getElementsByTagName("DB_LOCALE").item(0);
        if (n != null)
        	options.put("DB_LOCALE", n.getTextContent());
        
        n = rootElement.getElementsByTagName("NODEFDAC").item(0);
        if (n != null)
        	options.put("NODEFDAC", n.getTextContent());
        
        n = rootElement.getElementsByTagName("IFX_ISOLATION_LEVEL").item(0);
        if (n != null)
        	options.put("IFX_ISOLATION_LEVEL", n.getTextContent());
        
        n = rootElement.getElementsByTagName("IFX_LOCK_MODE_WAIT").item(0);
        if (n != null)
        	options.put("IFX_LOCK_MODE_WAIT", n.getTextContent());
        
    	return options;
    }
    
    /**
     * Sprema opcije u xml datoteku
     * 
     * @param options mapa u kojoj se nalaze opcije
     * @param f datoteka u koju se spremaju opcije
     */
    public void save(LinkedHashMap<String,String> options, File f)
    {
    	Document dom;
    	try
        {
        	dom = db.newDocument();
        } catch (Exception e)
        {
        	e.printStackTrace();
        	return;
        }
        
        Element rootElement = dom.createElement("configuration");
        dom.appendChild(rootElement);
        
        for (String s : options.keySet())
        {
        	Element ele = dom.createElement(s);
        	Text txt = dom.createTextNode(options.get(s));
        	ele.appendChild(txt);
        	rootElement.appendChild(ele);
        }
        
        //print to file
        try 
        {
        	OutputFormat format = new OutputFormat(dom);
        	format.setIndenting(true);
        	
        	XMLSerializer serializer = new XMLSerializer(new FileOutputStream(f), format);
        	
        	serializer.serialize(dom);
		} catch (IOException ie) {
			ie.printStackTrace();
		}
    }
}